package com.penghaisoft.microwms.producthand.web.controller.outwarehouse;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.ProSellOut;
import com.penghaisoft.microwms.common.data.model.ProSellOutSn;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProSellOutDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.product.service.IBaseDictionaryService;
import com.penghaisoft.microwms.product.service.IProSellOutService;
import com.penghaisoft.microwms.product.service.IProSellOutSnService;

/**
 * 销售出库 --手持
 * @author liwenjiang
 *
 */
@RestController
@RequestMapping(value = "/pro/sell/out")
public class ProSellOutController extends BaseController {
    @Autowired
    private IBaseDictionaryService baseDictionaryService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;//注入对象
    
    @Autowired
    private IProSellOutService proSellOutService;
    
    @Autowired
    private IProSellOutSnService proSellOutSnService;
    
    /**
     * 提单--输入出库单号查询手工出库表
     * @param proSellOut
     * @return
     */
    @PostMapping("loadBill")
    public HandResp confirm(@RequestBody ProSellOut proSellOut){
    	HandResp result = new HandResp();
    	Resp resp = proSellOutService.loadBill(proSellOut);
    	result.setCode(resp.getCode());
    	result.setMsg(resp.getMsg());
		return result;
    	
    }
    /**
     * 点击详情----查询 pro_sell_out表
     * @param proSellOut
     * @return
     */
    @PostMapping("info")
    public HandResp info(@RequestBody ProSellOutDto proSellOutDto){
    	HandResp result = new HandResp();
//    	ProSellOutSn proSellOutSn = new ProSellOutSn();
//    	proSellOutSn.setActiveFlag("1");
//    	proSellOutSn.setSellOutNo(proSellOutDto.getSellOutNo());
//    	List<ProSellOutSn> dataList = proSellOutSnService.findByAny(proSellOutSn);
    	ProSellOut proSellOut = new ProSellOut();
    	proSellOut.setActiveFlag("1");
    	proSellOut.setSellOutNo(proSellOutDto.getSellOutNo());
    	List<ProSellOut> dataList = proSellOutService.queryListByStatus(proSellOut);
    	result.setCode(Constant.SUCCESS);
    	result.setList(dataList);
		return result;
    }
    
    /**
     * 扫码
     * @param sellOutDto
     * @return
     */
    @PostMapping("scan")
    public HandResp scan(@RequestBody ProSellOutDto sellOutDto){
		HandResp result = new HandResp();
    	String key = Constant.PRO_SELLOUT_PREFIX + sellOutDto.getBarcode();
    	if(stringRedisTemplate.hasKey(key)){
    		result.setCode(Constant.FAIL);
    		result.setMsg("当前条码正在处理中，请不要重复扫描！");
			return result;
    	}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			TransResult transResult = new TransResult();
			try{
				transResult = proSellOutService.sellOutScanBarcode(sellOutDto);
				result.setCode(transResult.getCode());
				result.setMsg(transResult.getMsg());
				result.setList(transResult.getList());
			}catch(RuntimeException e){
				result.setCode(Constant.FAIL);
				result.setMsg(e.getMessage());
			}finally{
				stringRedisTemplate.delete(key);
			}

    	}
    	return result;
    }
    /**
     * 删除
     * @param sellOutDto
     * @return
     */
    @PostMapping("del")
    public HandResp del(@RequestBody ProSellOutDto sellOutDto){
    	HandResp resp = new HandResp();
    	String key = Constant.PRO_SELLOUT_PREFIX + sellOutDto.getBarcode();
    	if(stringRedisTemplate.hasKey(key)){
    		resp.setCode(Constant.FAIL);
    		resp.setMsg("当前条码正在处理，不要重复扫描");
    		return resp;
    	}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			TransResult result = new TransResult();
			try{
				result = proSellOutService.sellOutDelBarcode(sellOutDto);
				resp.setCode(result.getCode());
		        resp.setMsg(result.getMsg());
		        resp.setList(result.getList());
			}catch(RuntimeException e){
				resp.setCode(Constant.FAIL);
				resp.setMsg(e.getMessage());
			}finally{
				stringRedisTemplate.delete(key);//删除对象
			}

    	}
		return resp;
    }
    /**
     * 确认完成
     * @param sellOutDto
     * @return
     */
    @PostMapping("comfire")
    public HandResp complete(@RequestBody ProSellOutDto sellOutDto){
    	HandResp resp = new HandResp();
    	String key = Constant.PRO_SELLOUT_PREFIX + sellOutDto.getBarcode();
    	if(stringRedisTemplate.hasKey(key)){
    		resp.setCode(Constant.FAIL);
    		resp.setMsg("请等" + Constant.PRO_HAND_LOST_MINUTES + "分钟再操作！");
    	}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			TransResult result = new TransResult();
			try{
				result = proSellOutService.sellOutComplete(sellOutDto);
				resp.setCode(result.getCode());
		        resp.setMsg(result.getMsg());
		        resp.setList(result.getList());
			}catch(RuntimeException e){
				resp.setCode(Constant.FAIL);
				resp.setMsg(e.getMessage());
			}finally{
				stringRedisTemplate.delete(key);//删除对象  
			}
    	}
		return resp;
    }
    
}
